<?php

namespace App\Imports;

use App\Http\Models\Salesman;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class SalesmanImport implements ToCollection, WithHeadingRow
{
    use BaseImportTrait;

    private $required_field = ['工号', '姓名'];

    public function collection(Collection $rows)
    {
        foreach ($rows as $key => $row) {
            $row = $row->toArray();

            if ($key === 0) {
                $this->verifyField($row);
                $this->prepareBaseData();
            }

            $this->verifyNotNull($row, $key + 2);
            $this->verifyValueExist($row, $key + 2);

            $this->saveData($row);
        }
    }


    //准备基础数据，用来验证
    private function prepareBaseData()
    {
    }


    //验证字段值存在于数据库基础表中
    private function verifyValueExist(array $row, int $index)
    {
    }

    //保存数据
    private function saveData(array $row)
    {
        Salesman::updateOrCreate(
            [
                'job_number' => $row['工号'],
            ],
            [
                'name' => $row['姓名'],
            ]
        );
    }
}
